//
//  PhotoBrowserViewController.swift
//  PhotoBrowser
//
//  Created by haoran on 16/4/28.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

import UIKit

class PhotoBrowserViewController: UIViewController {

    // MARK:- 定义属性
    let ID = "PhotoBrowserCell"
    var shopArray : [Shop]?
    var indexPath : NSIndexPath?
    
    
    
    
    // MARK:- 懒加载
        // collectionView
    lazy var collectionView : UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout:PhotoBrowserCollectionLayout())
        // 两个按钮
    private lazy var saveBtn : UIButton = UIButton(title: "保  存", bgColor: UIColor.darkGrayColor(), fontSize: 16)
    private lazy var closeBtn : UIButton = UIButton(title: "关  闭", bgColor: UIColor.darkGrayColor(), fontSize: 16)
    
    
    override func loadView() {
        super.loadView()
        view.bounds.size.width += 15
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.设置UI界面
        setupUI()
       
        // 2.滚动到相应的位置
        collectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition: .Left, animated: false)
    }



}


// MARK:- 设置UI
extension PhotoBrowserViewController {

    func setupUI() {
        // 1.添加子控件
        view.addSubview(collectionView)
        view.addSubview(saveBtn)
        view.addSubview(closeBtn)
        
        // 2.设置控件的frame
        collectionView.frame = view.bounds
        
        let btnW : CGFloat = 90
        let btnH : CGFloat = 32
        let btnY : CGFloat = UIScreen.mainScreen().bounds.height - btnH - 20
        closeBtn.frame = CGRect(x: 20, y: btnY, width: btnW, height: btnH)
        let saveBtnX : CGFloat = UIScreen.mainScreen().bounds.width - 20 - btnW
        saveBtn.frame = CGRect(x: saveBtnX, y: btnY, width: btnW, height: btnH)
        
        // 3.设置两个按钮的属性
        closeBtn.addTarget(self, action:#selector(PhotoBrowserViewController.closeBtnClick), forControlEvents:.TouchUpInside)
        saveBtn.addTarget(self, action: #selector(PhotoBrowserViewController.saveBtnClick), forControlEvents: .TouchUpInside)
        
        // 4.设置collection的相关属性
       
        collectionView.registerClass(PhotoBrowserCell.self, forCellWithReuseIdentifier:ID )
        collectionView.dataSource = self
        collectionView.delegate = self
    }
    
}


// MARK:- 点击事件监听
extension PhotoBrowserViewController {
    @objc private func closeBtnClick() {
        dismissViewControllerAnimated(true, completion: nil)
    }


    @objc private func saveBtnClick()  {
        // 写入相册, 因为屏幕上只有一张图片,就只有一个cell
        // 1.取出正在屏幕中显示的Cell
        let cell = collectionView.visibleCells().first as! PhotoBrowserCell
        
        // 2.取出imageView
        let image = cell.imageView.image
        
        // 3.写入相册
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
        
    }
}

// MARK:- collectionView的数据源方法
extension PhotoBrowserViewController : UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // ?? 一般用在可选链后面
        // 如果前面的可选类型有一个没有值,那么直接使用??后面的值
        // 如果前面的可选类型都有值,那么对最终的解决进行解包
        return shopArray?.count ?? 0;
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath) as! PhotoBrowserCell
        
        // 设置cell的数据
        cell.shop = shopArray![indexPath.item]
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        closeBtnClick()
    }
}

